package cn.xclink.kernel.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.xclink.common.web.BaseController;
import cn.xclink.common.web.ExecuteResult;
import cn.xclink.kernel.entity.Dic;
import cn.xclink.kernel.entity.DicItem;
import cn.xclink.kernel.service.DicItemService;
import cn.xclink.kernel.service.DicService;

@Controller
@RequestMapping("system/dicItem")
public class DicItemController extends BaseController<Dic>{
	
	public static String LEFT_DIVID = "sys_dic_left_div";
	public static String RIGHT_DIVID = "sys_dic_right_div";

    @Autowired
    private DicItemService dicItemService;
    
    @Autowired
    private DicService dicService;
    
    @ModelAttribute
	public void init(Model model) {
		model.addAttribute("left_divid", LEFT_DIVID);
		model.addAttribute("right_divid", RIGHT_DIVID);
	}
    
    @RequestMapping
    public String list(DicItem dicItem, Model model, HttpServletRequest request,
    		@RequestParam(value="pageNum",required=false) Integer pageNum, 
    		@RequestParam(value="pageSize",required=false) Integer pageSize) {
		model.addAttribute("pageInfo", dicItemService.findPageInfo(dicItem,pageNum,pageSize));
		model.addAttribute("dicItem", dicItem);
		model.addAttribute("dic", dicService.get(dicItem.getDicId()));
        return "system/dicitem/list";
    }

    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String createForm(DicItem dicItem, Model model, HttpServletRequest request) {
    	model.addAttribute("dicItem", dicItem);
        return "system/dicitem/create";
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> create(DicItem dicItem, Model model, HttpServletRequest request) {
    	try {
			Integer result = dicItemService.save(dicItem);
			if (result == 1) {
				return  ExecuteResult.jsonReturn(200,RIGHT_DIVID);
			} else {
				return ExecuteResult.jsonReturn(300, false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			return ExecuteResult.jsonReturn(300, e.getMessage(),false);
		}
    }

    @RequiresPermissions("dic:edit")
    @RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
    public String editForm(@PathVariable("id") Long id, Model model,HttpServletRequest request) {
        model.addAttribute("dicItem",dicItemService.get(id));
        return "system/dicitem/edit";
    }

    @RequiresPermissions("dic:edit")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> edit(DicItem dicItem, Model model, HttpServletRequest request) {
    	try {
			Integer result = dicItemService.updateNotNull(dicItem);
			if (result == 1) {
				return ExecuteResult.jsonReturn(200,RIGHT_DIVID);
			} else {
				return ExecuteResult.jsonReturn(300, false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			return ExecuteResult.jsonReturn(300, e.getMessage(),false);
		}
    }

    @RequiresPermissions("dic:delete")
    @RequestMapping(value = "/{id}/delete", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> delete(@PathVariable("id") Long id, Model model, HttpServletRequest request) {
    	try {
			Integer result = dicItemService.delete(id);
			if (result == 1) {
				return ExecuteResult.jsonReturn(200,RIGHT_DIVID);
			} else {
				return ExecuteResult.jsonReturn(300, false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			return ExecuteResult.jsonReturn(300, e.getMessage(),false);
		}
    }
}
